﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Ftp
{
    /// <summary>
    /// 修改文件的最后修改时间
    /// </summary>
    internal class FtpMFMTCommand : FtpCommandAbs
    {
        /***********************************************************************************************************
        * 命令:
        * MFMT 20220318080240 FontsIcon.rar
        * 
        * 响应成功:
        * 213 20220318080240
        * 
        * 响应失败:
        * 421 the server is off-line or is going off-line
        * 501 Syntax error in parameters or arguments.This usually results from an invalid or missing file name.
        * 502 Command not implemented.This will be returned if MDTM support is not enable on the server.
        * 530 No user is currently authenticated on the command channel.
        * 553 File name not allowed,or file type banned
        *
        ************************************************************************************************************/


        public FtpMFMTCommand()
            : base()
        {

        }

        protected override string[] GetNames()
        {
            return new string[] { FtpCommands.MFMT };
        }

        protected override IFtpResponse PrimitiveExcute(FtpSession session, string command, string commandLine)
        {
            //Request: MFMT 20220318080240 FontsIcon.rar
            //Response: 213 20220318080240

            try
            {
                string[] strs = commandLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (strs.Length < 2)
                {
                    return new FtpCodeResponse(FtpStatusCodes.Code501, "Insufficient parameters.");
                }

                string filePath = session.GetLocalPath(strs[1]);
                if (!File.Exists(filePath))
                {
                    return new FtpCodeResponse(FtpStatusCodes.Code550, "not found file.");
                }

                string utcTimeStr = strs[0];
                var sb = new StringBuilder(19);
                for (int i = 0; i < utcTimeStr.Length; i++)
                {
                    if (i == 4 || i == 6)
                    {
                        sb.Append('-');
                    }
                    else if (i == 8)
                    {
                        sb.Append(' ');
                    }
                    else if (i == 10 || i == 12)
                    {
                        sb.Append(':');
                    }

                    sb.Append(utcTimeStr[i]);
                }

                utcTimeStr = sb.ToString();
                DateTime utctime = DateTime.Parse(utcTimeStr);
                File.SetLastAccessTimeUtc(filePath, utctime);
                return new FtpCodeResponse(FtpStatusCodes.Code213, "Command successful.");
            }
            catch (Exception ex)
            {
                return new FtpCodeResponse(FtpStatusCodes.Code451, ex.Message);
            }
        }
    }

}
